<?php

namespace App\Http\Controllers\Api;

use App\Http\Requests\PatientRequest;
use App\Services\Api\ApiService;
use App\Services\PatientService;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class PatientController extends Controller
{
    private $service;
    public function __construct(ApiService $apiService, PatientService $service)
    {
        $this->service = $service;
        parent::__construct($apiService);
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $showPage = $this->getShowPage();
        if (!empty(request()->input('showpage'))){
            $showPage = request()->input('showpage');
        }
        $this->service->repository->pushCriteria(app('Prettus\Repository\Criteria\RequestCriteria'));
        $patients = $this->service->repository->scopeQuery(function ($query){
            return $query->with('deportment', 'media', 'patientType', 'doctor');
        })->paginate($showPage);
        return $this->apiService->run(200, $patients);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(PatientRequest $request)
    {
        try{
            $patient = $this->service->repository->create($request->all());
            return $this->apiService->run(200, $patient);
        }catch (\Exception $exception){
            return $this->apiService->run(500);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
            $patient = $this->service->repository->find($id);
            if (empty($patient)) return $this->apiService->run(400);
            return $this->apiService->run(200, $patient);
    }


    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(PatientRequest $request, $id)
    {
        try {
            $patient = $this->service->repository->find($id)->update($request->all());
            if (empty($patient)) return $this->apiService->run(400);
            return $this->apiService->run(200);
        }catch (\Exception $exception){
            return $this->apiService->run(500);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $patient = $this->service->repository->delete($id);
        if (empty($patient)) return $this->apiService->run(400);
        return $this->apiService->run(200);
    }

    /**
     * @return mixed
     */
    public function showByUser(){
        $showPage = $this->getShowPage();
        if (!empty(request()->input('showpage'))){
            $showPage = request()->input('showpage');
        }
        $this->service->repository->pushCriteria(app('Prettus\Repository\Criteria\RequestCriteria'));
        $patients = $this->service->repository->scopeQuery(function ($query){
            return $query->with('deportment', 'media', 'patientType', 'doctor')->where(['user_id' => Auth::user()->id]);
        })->paginate($showPage);
        return $this->apiService->run(200, $patients);
    }


    /**
     * @return mixed
     */
    public function showByDoctor(){
        $showPage = $this->getShowPage();
        if (!empty(request()->input('showpage'))){
            $showPage = request()->input('showpage');
        }
        $this->service->repository->pushCriteria(app('Prettus\Repository\Criteria\RequestCriteria'));
        $patients = $this->service->repository->scopeQuery(function ($query){
            return $query->with('deportment', 'media', 'patientType', 'doctor')->where(['doctor_id' => $this->service->getDoctorId(Auth::user()->name)]);
        })->paginate($showPage);
        return $this->apiService->run(200, $patients);
    }

    /**
     * @return mixed
     */
    public function showByDeportment(){
        $showPage = $this->getShowPage();
        if (!empty(request()->input('showpage'))){
            $showPage = request()->input('showpage');
        }
        $this->service->repository->pushCriteria(app('Prettus\Repository\Criteria\RequestCriteria'));
        $patients = $this->service->repository->scopeQuery(function ($query){
            return $query->with('deportment', 'media', 'patientType', 'doctor')->where(['deportment_id' => Auth::user()->deportment_id]);
        })->paginate($showPage);
        return $this->apiService->run(200, $patients);
    }
}
